const express = require('express');
const router = express.Router();
const jwt = require('jsonwebtoken');

const JWT_SECRET = process.env.JWT_SECRET || 'your-secret-key';

// 登录验证路由
router.post('/login', (req, res) => {
  try {
    const { password } = req.body;
    const adminPassword = process.env.ADMIN_PASSWORD;

    // 检查密码是否匹配
    if (password === adminPassword) {

      // 生成JWT Token，设置过期时间为1小时
      const token = jwt.sign(
        { user: 'admin' },
        JWT_SECRET,
        { expiresIn: '1d' } // 设置Token有效期
      );


      res.json({
        code: 0,
        message: '登录成功',
        token
      });
    } else {
      res.status(401).json({
        code: 1,
        message: '密码错误',
      });
    }
  } catch (error) {
    res.status(500).json({
      code: 2,
      message: '服务器错误',
    });
  }
});


module.exports = router;